<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.licensePlate" placeholder="车牌号" clearable></el-input>
      </el-form-item>
      <el-form-item label="车辆所属">
        <el-select  style="width: 130px" v-model="dataForm.belong" placeholder="车辆所属">
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in vehicelOwnList"
            :key="item.value"
            :label="item.label"
            :value="+item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="能源类型">
        <el-select  style="width: 130px"  v-model="dataForm.energyType" placeholder="能源类型">
          <el-option
            label="全部"
            value="">
          </el-option>
          <el-option
            v-for="item in vehicelTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('tms:truck:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
<!--        <el-button v-if="isAuth('tms:truck:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"

      style="width: 100%;">
<!--      <el-table-column-->
<!--        type="selection"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        width="50">-->
<!--      </el-table-column>-->

      <el-table-column
        prop="licensePlate"
        header-align="center"
        align="center"
        label="车牌号">
      </el-table-column>
      <el-table-column
        prop="energyType"
        header-align="center"
        align="center"
        label="能源类型">
      <template slot-scope="scope">
        {{scope.row.energyType == 1?'燃油车':'电动车'}}
      </template>

      </el-table-column>
      <el-table-column
        prop="expireDate"
        header-align="center"
        align="center"
        label="年检日期">
      </el-table-column>
      <el-table-column
        width="150px"
        prop="type"
        header-align="center"
        align="center"
        label="车辆类型(吨位)">
      <template slot-scope="scope">
        {{scope.row.type == 0?'小型车辆':scope.row.type == 1?'中型车辆':scope.row.type == 2?'大型车辆':'特大型车辆'}}
      </template>

      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="车辆所属">
        <template slot-scope="scope">
          {{scope.row.belong ==1?'自有车辆':'外部车辆'}}
        </template>
      </el-table-column>
      <el-table-column
        width="100px"
        prop="defaultDriverName"
        header-align="center"
        align="center"
        label="驾驶员">
      </el-table-column>
      <el-table-column
        width="200px"
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="creator"
        header-align="center"
        align="center"
        label="创建人">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button  v-if="isAuth('tms:truck:update')"  type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button  v-if="isAuth('tms:truck:delete')"  type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" :trcukType="trcukType" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './truck-add-or-update'
  export default {
    data () {
      return {
        trcukType:[],
        vehicelOwnList:[{label:'自有车辆',value:1},{label:'外部车辆',value:2},],
        vehicelTypeList:[{label:'全部',value:0},{label:'燃油车',value:0},{label:'电动车',value:1},{label:'混动车',value:2}],
        dataForm: {
          energyType: '',
          licensePlate:'',
          belong:''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        driverList: []
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
      this.paramsList();
      this.getDriverList()
    },
    methods: {
      getDriverList() {
        this.$http({
          url: this.$http.adornUrl('/tms/driver/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 999
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.driverList = data.page.list

          }
        })
      },
      // 获取数据列表
      paramsList () {

        this.$http({
          url: this.$http.adornUrl('/sys/config/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 999
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.vehicelTypeList = JSON.parse(data.page.list.filter(item => item.paramKey === 'energyType')[0].paramValue)
            this.trcukType = JSON.parse(data.page.list.filter(item => item.paramKey === 'trcukType')[0].paramValue)

          }

        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/tms/truck/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'licensePlate': this.dataForm.licensePlate,
            'energyType': this.dataForm.energyType,
            'belong':this.dataForm.belong
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, this.vehicelTypeList, this.vehicelOwnList,this.driverList)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/tms/truck/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
